package com.sunfeng.netty.atguigu.code;

import java.io.Serializable;
import java.nio.ByteBuffer;

/**
 * @Author : 孙峰
 * @Description: 用户详情信息
 * @Date : 2021/10/19  16:25
 */
public class UserInfo implements Serializable {
    private static final long serialVersionID = 1L;

    private String userName;

    private int userID;

    public UserInfo() {
    }

    public UserInfo(String userName, int userID) {
        this.userName = userName;
        this.userID = userID;
    }

    public UserInfo builderUserID(int userID) {
        this.userID = userID;
        return this;
    }
    public UserInfo builderUserName(String userName) {
        this.userName = userName;
        return this;
    }
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getUserID() {
        return userID;
    }

    public void setUserID(int userID) {
        this.userID = userID;
    }


    public byte[] codeC() {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        byte[] userNameBytes = this.userName.getBytes();
        buffer.putInt(userNameBytes.length);
        buffer.put(userNameBytes);
        buffer.putInt(this.userID);
        buffer.flip();
        userNameBytes = null;
        byte[] result = new byte[buffer.remaining()];
        buffer.get(result);
        return result;
    }
    public byte[] codeC(ByteBuffer buffer) {
        buffer.clear();

        byte[] userNameBytes = this.userName.getBytes();
        buffer.putInt(userNameBytes.length);
        buffer.put(userNameBytes);
        buffer.putInt(this.userID);
        buffer.flip();
        userNameBytes = null;
        System.out.println("==========");
        byte[] result = new byte[buffer.remaining()];
        buffer.get(result);
        return result;
    }
}
